local ListView = class("ListView");
function ListView:ctor(args)
    self.lv_ = args.listView;
--    self.contentList_ = {};
    self.curSelectedItem = nil;
    self.curSelectedItemIndex = 1;
    self.setSelectedCbk = args.cbk or nil;
    self.unSelectable = false;
end

function ListView:addContent(content)
    local item = self.lv_:newItem();
    local widgetSize = content.nodeSize;
    item:setItemSize(widgetSize.width,widgetSize.height);
    item:addContent(content);
--    table.insert(self.contentList_,content);
    self.lv_:addItem(item);
end

function ListView:getAllContent()
--    return self.contentList_;
end

function ListView:removeItem(index, bAni)
    index = index or self.curSelectedItemIndex;
    mgrs.log:printNotice("del index = %d",index);
    bAni = bAni or true;
    local allItems = self:getAllItems();
    if index <= 0 or index > #allItems then
        return
    end
    local endIndex = index;
    if index > #allItems then
        return;
    end
    if index == #allItems then
        endIndex = index - 1;
    end
    local delItem = allItems[index];
    self.lv_:removeItem(delItem, bAni);
    if delItem == self.curSelectedItem then
        self.curSelectedItem = nil;
    end
    if endIndex ~= 0 then
        self:setSelectedByIndex(endIndex);
    end
end

function ListView:getCurSelectedaddContent()
    return self.curSelectedItem;
end

function ListView:getAllItems()
    return self.lv_.items_;
end

function ListView:gotoList(args)
    local isRunAction = args.ani or true;
    local num = args.index or 1;
    local cbk = args.cbk or nil;
    local scNode = self.lv_:getScrollNode();
    local scNodeRect = self.lv_:getScrollNodeRect();
    local rect = self.lv_:getViewRect();
    local allItems = self:getAllItems();

    local contentWidth = scNodeRect.width;
    local contentHeight = scNodeRect.height / #allItems;
    local pre = rect.height / contentHeight;

    local curPosX,curPosY = scNode:getPosition();
    if num + pre - 1 <= #allItems then
        --当前的cell可以置顶
        local length = scNodeRect.height - rect.height - (num - 1) * contentHeight;
        if isRunAction then
            if cbk then
                mgrs.action:beginAction(scNode):MoveTo(0.5,cc.p(curPosX,-length)):CallFunc(cbk):endAction();
            else
                mgrs.action:beginAction(scNode):MoveTo(0.5,cc.p(curPosX,-length)):endAction();
            end
        else
            scNode:setPositionY(-length);
        end
    else
        --滑动到底层
        if isRunAction then
            if cbk then
                mgrs.action:beginAction(scNode):MoveTo(0.5,cc.p(curPosX,0)):CallFunc(cbk):endAction();
            else
                mgrs.action:beginAction(scNode):MoveTo(0.5,cc.p(curPosX,0)):endAction();
            end
        else
            scNode:setPositionY(0);
        end
    end
end

function ListView:setSelectedByIndex(index)
    local curItem = self:getAllItems()[index];
    if self.curSelectedItem then
        if curItem == self.curSelectedItem then
            return;
        end
        self.curSelectedItem:getContent():onSelect(false);
    end
    self.curSelectedItem = curItem;
    self.curSelectedItem:getContent():onSelect(true);
    self.curSelectedItemIndex = index;
    if self.setSelectedCallBack then
        self.setSelectedCallBack(index);
    end
end

function ListView:setSelectedCallBack(cbk)
    self.setSelectedCallBack = cbk;
end

function ListView:setUnselectable(v)
    self.unSelectable = v;
end

function ListView:reload()
    self:setTouch();
    self:setBounceable(true);
    self.lv_:reload();
    if not self.unSelectable then
        self:setSelectedByIndex(self.curSelectedItemIndex);
    end
end

function ListView:setBounceable(b)
    self.lv_:setBounceable(b);
end

function ListView:setTouch()
    self.lv_:onTouch(function(event)
        local name = event.name;
        local itemPos = event.itemPos;
        local item = event.item;
        if name == "clicked" then
            if itemPos and item then
                self.curSelectedItemIndex = itemPos;
                self:setSelectedByIndex(itemPos);
            end
        end
    end)
end

return ListView;

--endregion
